//LINES (TETRIS) main file

#include "main.h"
#include "Game_view.h"
#include "Game_Logic.h"

#define SAFE_DELETE(p) {if(p) {delete(p);(p)=NULL;}}

Game_view* gp_gameV = NULL;
Game_Logic* gp_gameL = NULL;

//Initiates, controls and destroys the game
int main(int argc, char **argv)
{
    int returncode = 0;
    bool stop = true;
    Uint32 timer_frames = SDL_GetTicks();

    //Initialize random generator
    srand(time(NULL));
    rand();

    try
    {
        gp_gameV = new Game_view(); //Initialize game_view
        gp_gameL = new Game_Logic(gp_gameV); //Initialize game_logic


        while (stop)
        {
            if((stop = gp_gameL->Update()));
            {
                if(SDL_GetTicks()-timer_frames >= 1000/60) //60 frames/sec
                {
                    gp_gameV->Render();
                    timer_frames = SDL_GetTicks();
                }

            }

        }

    }catch(std::string e){

        std::cout<<"Critical error!: "<<e<<std::endl;
        returncode = 1;
    }

    //Relasing objects (reverse allocation order to avoid deadlock!)
    SAFE_DELETE(gp_gameL);
    SAFE_DELETE(gp_gameV);

    return returncode;
}
